package com.youtellv1.mapper;

import com.youtellv1.entity.Child;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ChildMapper extends BaseMapper<Child>{
    @Select("select * from tb_child order by cid limit #{offset}, #{limit}")
    List<Child> findAllChild(@Param("offset") int offset, @Param("limit") int limit);

    @Select("select * from tb_child where cid = #{cid}")
    Child findChild(Child child);

    @Select("select * from tb_child where cid = #{cid}")
    Child findById(@Param("cid") Integer cid);

    @Select("select * from tb_child where cid = #{cid}")
    Child findChildById(@Param("cid") Integer cid);

    @Select("select cname from tb_child where cid = #{cid}")
    Child findNameByCid (@Param("cid") Integer cid);

    @Insert("insert into tb_child(cname, csex, cage, cparnt, ctel, flag) values (#{cname}, #{csex}, #{cage}, #{cparnt}, #{ctel}, #{flag})")
    void save(Child child);

    @Update("update tb_child set flag = #{flag} where cid = #{cid}")
    void updateFlag(@Param("cid") Integer cid, @Param("flag") String flag);

    @Update("<script>" +
            "update tb_child " +
            "<set>" +
            "    <if test='cname != null'>cname = #{cname},</if>" +
            "    <if test='csex != null'>csex = #{csex},</if>" +
            "    <if test='cage != null'>cage = #{cage},</if>" +
            "    <if test='cparnt != null'>cparnt = #{cparnt},</if>" +
            "    <if test='ctel != null'>ctel = #{ctel},</if>" +
            "    <if test='flag != null'>flag = #{flag}</if>" +
            "</set>" +
            "where cid = #{cid}" +
            "</script>")
    void update(Child child);

    @Delete("delete from tb_child where cid = #{cid}")
    void delete(Child child);

    @Select("select count(*) from tb_child where flag = 1")
    Integer childCount();
}
